import { Component, Host, ChangeDetectionStrategy, ChangeDetectorRef } from "@angular/core";
import { AppComponent } from "../../../../../../app.component";
import { CompanyGovernanceService } from "../../companyGovernance.service";
import { Subscription } from "rxjs";
import { CompanyService } from "../../../company.service";
import { RelativePerformanceFullModel } from "./models/relativePerformanceFull.model";
import { RelativePerformanceSummaryModel } from "./models/relativePerformanceSummary.model";
import { OverallPerformanceModel } from "../../models/overallPerformance.model";
import { AssessmentList } from "../../../../../../shared/models/assessment.enum";
import { GovernanceProfileModel } from "../../models/governanceProfile.model";
import { UtilsService } from "../../../../../../shared/services/utils.service";
import { Outlook } from "../../../../../../shared/models/outlook.enum";
import { BaseComponent } from "../../../../../../shared/components/baseComponent/base.component";
import { ClusterModelWrapper } from "../../../../riskCluster/cluster.model";
import { AnalyticsService } from "../../../../../../shared/services/googleAnalytics.service";
import { GoogleAnalyticsBeacon } from "../../../../../../shared/models/googleAnalytics.model";

/**
 * Company governance summary component
 *
 * Author: Sorin Spiru
 * Date: 26.02.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'companyGovernanceSummary.component.html',
    styleUrls: [
        '../../../company.component.scss',
        'companyGovernanceSummary.component.scss'
    ],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class CompanyGovernanceSummaryComponent extends BaseComponent {
    public profileSub: Subscription;
    public performanceSub: Subscription;
    public relativeSub: Subscription;
    public assessmentList: typeof AssessmentList = AssessmentList;
    public outlook: typeof Outlook = Outlook;
    public getUrlFriendlyName = UtilsService.getUrlFriendlyName;
    public summaryClusters : ClusterModelWrapper[];

    public governanceProfile: GovernanceProfileModel;
    public overallPerformance: OverallPerformanceModel;
    public fullRelativePerformance: RelativePerformanceFullModel;
    public summaryRelativePerformance: RelativePerformanceSummaryModel;

    constructor(
        private cdRef: ChangeDetectorRef,
        public companyService: CompanyService,
        public companyGovernanceService: CompanyGovernanceService,
        private analyticsService: AnalyticsService,
        @Host() private rootComponent: AppComponent) {
        super(cdRef);
    }

    ngOnInit() {
        this.profileSub = this.companyGovernanceService.governanceProfile.subscribe(data => {
            this.governanceProfile = data;
            this.cdRef.markForCheck();
        }, error => this.errorCallback(_ => { this.governanceProfile = undefined; }, error));

        this.performanceSub = this.companyGovernanceService.overallPerformance.subscribe(data => {
            this.overallPerformance = data;
            this.cdRef.markForCheck();
            this.rootComponent.finishLoading();
        }, error => this.errorCallback(_ => { this.overallPerformance = undefined; }, error));

        this.relativeSub = this.companyGovernanceService.relativePerformance.subscribe(data => {
            this.fullRelativePerformance = data as RelativePerformanceFullModel;
            this.cdRef.markForCheck();
        }, error => this.errorCallback(_ => { this.fullRelativePerformance = undefined; }, error));


        this.companyService.company.subscribe(company => {
            this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('OnscreenCorporateGovernanceReport', 'Research', { CompanyId: company.id }));
            this.companyGovernanceService.getGovernanceRelativePerformance(company.id, false).subscribe(data => {
                this.summaryRelativePerformance = data as RelativePerformanceSummaryModel;
                this.cdRef.markForCheck();
            }, error => this.errorCallback(_ => { this.summaryRelativePerformance = undefined; }, error));
            this.companyGovernanceService.getGovernanceSummaryBenchmark(company.id).subscribe(data => {
                this.summaryClusters = this.getSummaryBenchmarkClusters(data[0].thresholds);
                this.cdRef.markForCheck();
            });
        });
    }

    private getSummaryBenchmarkClusters(thresholds:any) : ClusterModelWrapper[]
    {
        let clustersSummary : ClusterModelWrapper[] = [];
        clustersSummary.push( new ClusterModelWrapper(5, '5', 'Laggard', 'Laggard', '#F0AB01', thresholds[0]));
        clustersSummary.push(new ClusterModelWrapper(4, '4', 'Underperformer', 'Underperf.', '#F3BC33',  thresholds[1]));
        clustersSummary.push(new ClusterModelWrapper(3, '3', 'Average Performer', 'Avg. Perf.', '#F6CD66',  thresholds[2]));
        clustersSummary.push(new ClusterModelWrapper(2, '2', 'Outperformer', 'Outperf.', '#F9DD99', thresholds[3]));
        clustersSummary.push(new ClusterModelWrapper(1, '1', 'Leader', 'Leader', '#CCCCCC', 100 ));
        return clustersSummary;
    }

    ngOnDestroy() {
        this.profileSub.unsubscribe();
        this.performanceSub.unsubscribe();
    }

    public getPercentileOrdinal(): string {
        return UtilsService.getOrdinalNameEnd(this.summaryRelativePerformance.percentile);
    }

    public getIssueIcon( issueName: string ): string {
        return UtilsService.getIconFilename(issueName, 'governance');
    }
}
